package com.design.jdk;

public class StringTest {

    public static void main(String[] args) {

        // 在jdk层面方法区有个常量池，
        String s1 = "hello"; // 声明的时候创建了两个对象， hello在编译阶段声明，s1在运行阶段声明
        String s2 = "hello";
        String s3 = "he" + "llo";
        // s1和s2 指向同一个常量地址
        System.out.println(s1 == s2);   //true
        System.out.println(s1 == s3);   //true
        // new String() 重新分配内存
        String s4 = "hel" + new String("lo");   //false
        System.out.println(s1 == s4);

        String s5 = new String("hello");
        System.out.println(s1 == s5);   // false

        System.out.println(s4 == s5);   // false
        String s6 = s5.intern();
        System.out.println(s1 == s6);   // true

        String s7 = "h";
        String s8 = "ello";
        String s9 = s7 + s8;
        System.out.println(s1 == s9);
    }
}
